// *** LICENSE HEADER ***
// Filename: ./toggle.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "toggle.h"

GLuint toggle::toggleTexture = 0;

toggle::toggle(int newId, int posX, int posY, bool initToggled):
    guielement(posX, posY, "", newId) {
    toggled = initToggled;
    clicked = false;
}

toggle::~toggle(){}

void toggle::Draw(fonts* fontDrawer){
    if (!enabled) return;

    //Disable blending and textures
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, toggleTexture);

    //Draw the toggle background
    glBegin(GL_TRIANGLES);
    glColor3ub(r,g,b);
    glTexCoord2f(1.0f, 1.0f); glVertex2i(x, _HEIGHT-y);
    glTexCoord2f(0.0f, 1.0f); glVertex2i(x+20, _HEIGHT-y);
    glTexCoord2f(1.0f, 0.0f); glVertex2i(x, _HEIGHT-y-20);
    glTexCoord2f(0.0f, 1.0f); glVertex2i(x+20, _HEIGHT-y);
    glTexCoord2f(0.0f, 0.0f); glVertex2i(x+20, _HEIGHT-y-20);
    glTexCoord2f(1.0f, 0.0f); glVertex2i(x, _HEIGHT-y-20);
    glEnd();

    //Draw the border of the toggle
    glDisable(GL_TEXTURE_2D);
    glBegin(GL_LINE_STRIP);
    if (active)
        glColor3ub(255,80,80);
    else
        glColor3ub(80,80,80);
    glVertex2i(x, _HEIGHT-y);
    glVertex2i(x+20, _HEIGHT-y);
    glVertex2i(x+20, _HEIGHT-y-20);
    glVertex2i(x, _HEIGHT-y-20);
    glVertex2i(x, _HEIGHT-y);
    glEnd();

    //Draw a large "X" inside the toggle if it is toggled
    if (toggled)
        fontDrawer->Print(FT_MENU, "X", x + 7, _HEIGHT - y - 10 - _FONT_SIZE_MENU/2+2, _FONT_SIZE_MENU, toggle::textColor);
}

int toggle::Click(int clickX, int clickY){
    if (!enabled) return 0;
    if (clickX >= x && clickX <= x + 20 && clickY >= y && clickY <= y + 20) {
        clicked = true;
        return id;
    }
    else
        return 0;
}

int toggle::Release(int clickX, int clickY){
    if (!enabled || !clicked) {
        clicked = false;
        return 0;
    }
    clicked = false;
    if (clickX >= x && clickX <= x + 20 && clickY >= y && clickY <= y + 20) {
        toggled = !toggled;
        return id;
    }
    else
        return 0;

}

bool toggle::GetToggled(){
    return toggled;
}

void toggle::LoadTexture(){
    toggleTexture = ::LoadTexture("toggle_back.png");
}

void toggle::UnloadTexture(){
    ::UnloadTexture(toggleTexture);
}
